
App({
  globalData: {
    urlpath: "https://www.stringzc.top",
    uid: "",
    name:"",
    photo:"",
    power:0,
    socketUrl: "wss://www.stringzc.top/wss/allnums/", // socketUrl
    timeout: 10000, // 延迟
    socketHeartTimer: null,
    callback: function () {}, // socket连接回调函数
    socketClientTimer: null, // socket连接定时器
    isSocketConnect: false, // 当前socket是否连接
  },
  onHide() {
    this.closeSocketFunction()
  },
  onShow(){
    var that = this
      const openid = wx.getStorageSync('user_Datas');
      console.log(openid)
      var uid = openid.uid
      if (!uid) {
          this.weixinlogin()
        } else {
            that.globalData.uid =openid.uid
            that.globalData.power = openid.power
            that.globalData.photo = openid.photo
            that.globalData.name = openid.name
        }
      if(!this.globalData.isSocketConnect){
        this.connectSocketFunciton()
      }
  },
  weixinlogin(){
    // console.log("asdsad")
    var that = this
    wx.login({
      success: res => {
        that.helper({
          url:that.globalData.urlpath + "/weixin/login",
          data:{
            code:res.code
          },
          success(res){
            console.log(res)
            that.globalData.uid = res.data.userdata.uid
            that.globalData.power = res.data.userdata.power
            that.globalData.photo = res.data.userdata.url
            that.globalData.name = res.data.userdata.name
            if(that.globalData.power){
            try{
              wx.setStorageSync('user_Datas', {"uid":res.data.userdata.uid,"power":res.data.userdata.power,"photo":res.data.userdata.url,"name":res.data.userdata.name })
              console.log('写入value成功')
            }catch (e) {
              console.log('写入value发生错误')
            }
          }
            
          }
        })
      },
      fail(){
        console.log("rrrr")
      }
      
    })
  },
  helper(args) {
    var header = {
      'content-type': 'application/json',
      'Accept': 'application/json',
      'cookie': wx.getStorageSync("sessionid")
    };
    wx.request({
      url: args.url,
      method: "POST",
      header: header,
      data: args.data,
      success(res) {
        var cookie = res.header["Set-Cookie"];
        if (cookie != null) {
          wx.setStorageSync("sessionid", res.header["Set-Cookie"]);
        }
        if (args.success) args.success(res);
      },
      complete(res) {
        if (args.complete) args.complete(res);
      },
      fail(res) {
        if(args.fail) args.fail(res);
      }
    })
  },
  connectSocketFunciton(){
    var that = this
    var socketUrl = that.globalData.socketUrl
    wx.connectSocket({
      url: socketUrl,
      success: (res => {
        // 标识socket已连接
        that.globalData.isSocketConnect = true
        // 初始化连接监听
        that.initEventHandler()
      }),
      fail: (fail => {})
  })
  },
  initEventHandler() {
    var that = this
    // socket连接开启
    wx.onSocketOpen((result) => {
      // 开启心跳
      that.startHeart()
    })
    // socket连接关闭
    wx.onSocketClose((res) => {
      if (that.globalData.isSocketConnect) {
        that.connectSocketFunciton()
      }
    })
    // 接收socket消息
    wx.onSocketMessage((res) => {
      console.log("onSocketMessage", res)
      //第一次消息如果为init就绑定uid
      var data = JSON.parse(res.data)
      //这里data可以根据后端返回的数据做一些处理 根据自己的需求做出对应的处理
      // 全局socket接收消息的方法回调
      that.globalData.callback(res)
    })
  },
  //发送心跳
  startHeart() {
    if (this.globalData.isSocketConnect) {
      this.globalData.socketHeartTimer = setInterval(() => {
        var sendsdata = {"event":"getnum","openid":this.globalData.uid}
        
        wx.sendSocketMessage({
          data: JSON.stringify(sendsdata),
          success: function (res) {},
          fail: function (res) {}
        })
      }, 25 * 1000)
    } else {
      clearTimeout(this.globalData.socketHeartTimer)
    }
  },
  //此页面关闭socket和定时器的函数
  closeSocketFunction: function () {
    if (this.globalData.isSocketConnect) {
      // 标识socket已断开
      this.globalData.isSocketConnect = false
      // 关闭socket连接
      wx.closeSocket()
      // 关闭socket心跳定时器
      clearInterval(this.globalData.socketHeartTimer)
    }
  },
})

